const UserService = require("../service/UserService");
const {decrypt} = require("../utils/encrypt");
const {generateAccessToken,generateRefreshToken} = require('../utils/jwt')
const UserController = {
    // 用户注册逻辑
    userRegister: async (req,res,next)=>{
        let userinfo = {...req.body};
        let addResult = await UserService.addUser(userinfo);
        let findResult = await UserService.findUser(userinfo);
        console.log(addResult,findResult)
        if(addResult.code===200){
            // 用于生成token的payload
            let payload = {
                id:findResult._id,
                username:findResult.username,
                email:findResult.email,
            }
            // 用于返回用于信息的对象
            let resultUserInfo  = {
                id:findResult._id,
                username:findResult.username,
                email:findResult.email,
            }
            // 如果注册成功，则生成访问令牌和刷新令牌
            let accessToken = generateAccessToken(payload)
            let refreshToken = generateRefreshToken(payload);
            
            res.cookie('refreshToken',refreshToken,{
                httpOnly:true,
                maxAge:1000*60*60*24*7,//7天有效期
                domain:'localhost',
                sameSite:'lax',//允许跨端口请求
                path:'/'//所有路径都可以访问
            });
            res.send({
                token:accessToken,
                code:200,
                msg:"注册成功",
                data:resultUserInfo
            });
        }else{
            res.json(addResult)
        }
    },
    // 用户登录逻辑
    userLogin: async (req, res, next)=>{
        let {email,password} = req.body;
        let findResult = await UserService.findUser({email})
        if(findResult){
            // 如果用户名存在，则判断密码是否正确
            let isMatch = await decrypt(password,findResult.password);
                // 用于生成token的payload
                let payload = {
                    id:findResult._id,
                    username:findResult.username,
                    email:findResult.email,
                }
                // 用于返回用于信息的对象
                let resultUserInfo  = {
                    id:findResult._id,
                    username:findResult.username,
                    email:findResult.email,
                }
            if(isMatch){
                // 密码正确，返回用户信息
                let accessToken = generateAccessToken(payload);
                let refreshToken = generateRefreshToken(payload);
                res.cookie('refreshToken',refreshToken,{
                    httpOnly:true,
                    maxAge:1000*60*60*24*7,//7天有效期
                    domain:'localhost',
                    sameSite:'lax',//允许跨端口请求
                    path:'/'//所有路径都可以访问
                });
                res.send({
                    token:accessToken,
                    code:200,
                    msg:"登录成功",
                    data:resultUserInfo
                });
            }else{
                res.send({
                    code:400,
                    msg:"密码错误,请重新登录"
                });
            }
        }else{
            res.send({
                code:400,
                msg:"该用户名未注册，请先注册"
            });
        }
    },
    // 获取用户信息
    userProfile: async (req,res,next)=>{
        let {email} = req.body;
        let findResult = await UserService.profileInfo({email})
        if(findResult){
            let resultUserInfo  = {
                id:findResult._id,
                username:findResult.username,
                email:findResult.email,
            }
            res.send({
                code:200,
                msg:"获取用户信息成功",
                data:resultUserInfo
            });
        }
    },
    // 退出登录
    userLogOut: async (req,res,next)=>{
        res.clearCookie('refreshToken');
        res.send({
            code:200,
            msg:"退出登录成功"
        });
    }
}

module.exports = UserController;